﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using Lost_and_Found.Models.DB;
using Lost_and_Found.Models.ViewModel;

namespace Lost_and_Found.Models.ObjectManager
{
    public class ItemManager
    {
        LAFEntities db = new LAFEntities();
        public List<AttributeSuggest> getListAttribute(int idItem)
        {
            List<AttributeSuggest> listAttribute = new List<AttributeSuggest>();
            // return list AttributesOfItems 
            var list = db.AttributesOfItems.Where(i => i.ItemSuggestID == idItem);
            foreach (var it in list)
            {
                int x = it.AttributeSuggestID;
                listAttribute.Add(db.AttributeSuggest.Find(x));
            }
            return listAttribute;
        }
        public string getAttribute(int idItem)
        {
            List<AttributeSuggest> list = this.getListAttribute(idItem);
            string at = "";
            int size = list.Count;
            for (int i = 0; i < size; i++)
            {
                if (i == (size - 1))
                {
                    at += list.ElementAt(i).Name;
                }
                else
                {
                    at += list.ElementAt(i).Name + " , ";
                }
            }
            return at;
        }
        public void createSuggestItem(ItemSuggestModel item)
        {
            ItemSuggest isuggest = new ItemSuggest { Name = item.Name };
            db.ItemsSuggest.Add(isuggest);
            db.SaveChanges();
            List<string> list = this.split(item.Attribute);
            this.addSuggestAttribute(list);
            foreach (var attName in list)
            {
                var att = db.AttributeSuggest.Single(a => a.Name == attName);
                db.AttributesOfItems.Add(new AttributesOfItem { ItemSuggestID = isuggest.ID, AttributeSuggestID = att.ID });
                db.SaveChanges();
            }


        }
        /* split string of attributes to some attribute*/
        public List<string> split(string pattern)
        {

            List<string> list = new List<string>();
            if (pattern != "" && pattern != null)
            {
                string p = " {0,}, {0,}";
                Regex myRegex = new Regex(p);
                list = myRegex.Split(pattern).ToList();
                return list;
            }
            else return list;

        }
        /* Add attribute which not exist into the database*/
        public void addSuggestAttribute(List<string> list)
        {

            foreach (var att in list)
            {
                if (!db.AttributeSuggest.Any(a => a.Name == att))
                {
                    db.AttributeSuggest.Add(new AttributeSuggest { Name = att });
                    db.SaveChanges();
                }
            }
        }
        public void deleteAllAttributeOfItem(int ItemID)
        {
            var linkToAttribute = db.AttributesOfItems.Where(l => l.ItemSuggestID == ItemID).ToList();
            foreach (var x in linkToAttribute)
            {
                db.AttributesOfItems.Remove(x);
            }
            db.SaveChanges();
        }
        public void deleteAttribute(int attributeID)
        {
            var listDelete = db.AttributesOfItems.Where(a => a.AttributeSuggestID == attributeID);
            foreach (var a in listDelete)
            {
                db.AttributesOfItems.Remove(a);
                db.SaveChanges();
            }
            AttributeSuggest del = db.AttributeSuggest.Find(attributeID);
            if (del == null) return;
            db.AttributeSuggest.Remove(del);
            db.SaveChanges();
        }

        public void editItem(int ItemID, string attributes)
        {
            // Xóa tât cả các liên kết item với attributes
            this.deleteAllAttributeOfItem(ItemID);
            // Lấy list chỉnh sửa
            List<string> list = this.split(attributes);
            if (list != null)
            {
                this.addSuggestAttribute(list);

                foreach (var attName in list)
                {
                    // Lấy thuộc tính có tên trong list
                    var att = db.AttributeSuggest.Single(a => a.Name == attName);
                    // Thêm  vào nếu chưa tồn tại
                    if (!db.AttributesOfItems.Any(z => (z.AttributeSuggestID == att.ID && z.ItemSuggestID == ItemID)))
                    {
                        db.AttributesOfItems.Add(new AttributesOfItem { ItemSuggestID = ItemID, AttributeSuggestID = att.ID });
                    }
                    db.SaveChanges();
                }
            }
        }
    }
}